package com.wealth.scoremarket.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wealth.commodity.entity.Goods;
import com.wealth.commodity.service.IGoodsService;
import com.wealth.member.entity.Membership;
import com.wealth.member.service.IMembershipService;
import com.wealth.merchant.service.IMerchantService;
import com.wealth.page.entity.Ad;
import com.wealth.page.enums.AdTypeEnum;
import com.wealth.page.service.IADService;
import com.wealth.platform.entity.MerchantRate;
import com.wealth.platform.entity.PlatformActivity;
import com.wealth.platform.service.IMerchantRateService;
import com.wealth.platform.service.IPlatformActivityService;
import com.wealth.system.web.controller.BaseController;

/**
 * 积分商场
 * @author zjw
 *
 */
@Controller
@RequestMapping(value = "/score/market")
public class ScoreMarketController extends BaseController {
 
	Logger logger = Logger.getLogger(ScoreMarketController.class);
	
	@Autowired
	IADService iADService;
	
	@Autowired
	IMembershipService iMembershipService;

	@Autowired
	IGoodsService iGoodsService;
	
	@Autowired
	private IMerchantRateService merchantRateService;
	
	@Autowired
	private IPlatformActivityService iPlatformActivityService;
	
	
	@Autowired
	private IMerchantService iMerchantService;
	/**
	 * 积分商场首页
	 * @return
	 */
	@RequestMapping(value = "/index")
	public String index(Model model){
		
		try{
//			
//			//积分商城首页轮播图
//			List<Ad> adList = iADService.getAdByCorpIdAndTypeAndStatus(corpid, AdTypeEnum.SCOREMARKET.getValue(), 1);
//			model.addAttribute("adList", adList);
//			
//			//会员信息 
//			Membership membership = iMembershipService.getMemberShipByMemberIdAndCorpId(memberid,corpid);
//			model.addAttribute("membership", membership);
//			
//			//积分一元转换率
//			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
//			model.addAttribute("merchantRate", merchantRate);
//			
//			//虚拟商品 
//			Goods goods = new Goods();
//			goods.setIsVirtual(1);
//			 
//	        Page<Goods>  goodsPage = iGoodsService.getGoodsApi(0, 4, goods);
//	        model.addAttribute("goodsPage", goodsPage);
//	        //实物商品
//	        goods.setIsVirtual(0);
//	        Page<Goods>  marketPage = iGoodsService.getGoodsApi(0, 4, goods);
//	        model.addAttribute("marketPage", marketPage); 
//			 
//			 return "scoremarket/index";
//			 
			 
		  return "scoremarket/notstart";
		}catch(Exception e){
			logger.error("积分商场首页错误！！！", e);
			return "system/500";
		}	
	}
	
	
	/**
	 * 商品详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/goods/details/{id}")
	public String goodsDetails (Model model,@PathVariable Long id){
		
     try{
			//TODO商品详情
    	    Goods goods = iGoodsService.get(id);
    	    model.addAttribute("goods", goods);
    	    //积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			model.addAttribute("merchantRate", merchantRate);
			
			return "scoremarket/goods/details";
		}catch(Exception e){
			logger.error("查看商品详情错误！！！", e);
			return "system/500";
		}	
	}
	
	
	
	
	
	 /**
	  * 积分超市商品列表
	  * @param model
	  * @param id
	  * @return
	  */
	@RequestMapping(value = "/goods/list",method = RequestMethod.GET )
	public String goodsList (Model model){
		
     try{
    	     
	        Page<Goods>  goodsPage = iGoodsService.getGoodsApi(0, 10, null);
	        model.addAttribute("goodsPage", goodsPage); 
	        //积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			model.addAttribute("merchantRate", merchantRate);
			if(goodsPage.getContent().size()>0){
			model.addAttribute("sortno", goodsPage.getContent().get(goodsPage.getContent().size()-1).getId());
			}
			return "scoremarket/goods/list";
		}catch(Exception e){
			logger.error("积分超市商品列表错误！！！", e);
			return "system/500";
		}	
	}
	
	
	 /**
	  * 积分超市商品数据
	  * @param model
	  * @param id
	  * @return
	  */
	@RequestMapping(value = "/goods/listJson",method = RequestMethod.GET )
	@ResponseBody
	public Page<Goods> goodsListJson (Model model,HttpServletRequest request,Goods goods){
		
    try{
    	    String sortno  = request.getParameter("sortno");
    	     
    	    if(sortno==null || sortno ==""){
    	    	sortno = "0"; 
    	    }
	        Page<Goods>  goodsPage = iGoodsService.getGoodsApi(Integer.parseInt(sortno), 10, goods);
	        model.addAttribute("goodsPage", goodsPage); 
	       
			return goodsPage;
		}catch(Exception e){
			logger.error("积分超市商品数据错误！！！", e);
			return null;
		}	
	}
	
	
	 /**
	  * 积分超市限时抢商品页面
	  * @param model
	  * @param id
	  * @return
	  */
	@RequestMapping(value = "/goods/limitTimelist",method = RequestMethod.GET )
	public String goodslimitTimeList (Model model){
		
    try{
    	    Goods goods = new Goods();
	        goods.setLimitType(1);
	        Page<Goods>  goodsPage = iGoodsService.getGoodsApi(0, 10, goods);
	        model.addAttribute("goodsPage", goodsPage); 
	        
	        //0=满减  1=限时抢购
	       List<PlatformActivity> listActivity =  iPlatformActivityService.findPlatformActivityByType(1);
	   	   if(listActivity.size()>0){
	       model.addAttribute("listActivity", listActivity.get(0));
	   	   }
	   	   //积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			model.addAttribute("merchantRate", merchantRate);
			if(goodsPage.getContent().size()>0){
			model.addAttribute("sortno", goodsPage.getContent().get(goodsPage.getContent().size()-1).getId());
			}
			return "scoremarket/goods/limittimelist";
		}catch(Exception e){
			logger.error("积分超市限时抢商品页面错误！！！", e);
			return "system/500";
		}	
	}
	
	
	/**
	  * 积分超市限量抢商品页面
	  * @param model
	  * @param id
	  * @return
	  */
	@RequestMapping(value = "/goods/limitnumlist",method = RequestMethod.GET )
	public String goodslimitnumList(Model model){
		
   try{ 
			Goods goods = new Goods();
		    goods.setLimitType(2);
	        Page<Goods>  goodsPage = iGoodsService.getGoodsApi(0, 10, goods);
	        model.addAttribute("goodsPage", goodsPage); 
	        //积分一元转换率
			MerchantRate merchantRate = merchantRateService.findMerchantRateByCorpId(corpid);
			model.addAttribute("merchantRate", merchantRate);
			if(goodsPage.getContent().size()>0){
			model.addAttribute("sortno", goodsPage.getContent().get(goodsPage.getContent().size()-1).getId());
			}
			return "scoremarket/goods/limitnumlist";
		}catch(Exception e){
			logger.error("积分超市限时抢商品页面错误！！！", e);
			return "system/500";
		}	
	}
	
	
	public static String getDatePoor(Date endDate, Date nowDate) {  
		long nd = 1000 * 24 * 60 * 60;  
		long nh = 1000 * 60 * 60;   
		long nm = 1000 * 60;   
	    long ns = 1000;    
		// 获得两个时间的毫秒时间差异   
		long diff = endDate.getTime() - nowDate.getTime();    
		// 计算差多少天   
		long day = diff / nd;   
		// 计算差多少小时    
		long hour = diff % nd / nh;    
		// 计算差多少分钟    
		long min = diff % nd % nh / nm;   
		// 计算差多少秒//输出结果    
		long sec = diff % nd % nh % nm / ns;    
		return day + "天" + hour + "小时" + min + "分钟"+sec+"秒";
	}
 
	 
 
}
